﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class ManualTool_LingYong_Return : Form
    {
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        string str, 項次, 手工具編號, 手工具名稱, 領用日期, 領用部門, 領用人, 受理人, 歸還日期, 領用數量;
        public ManualTool_LingYong_Return(string str, string 項次, string 手工具編號, string 手工具名稱, string 領用日期, string 領用部門, string 領用人, string 受理人, string 歸還日期, string 領用數量)
        {
            InitializeComponent();
            this.受理人 = 受理人;
            this.手工具名稱 = 手工具名稱;
            this.手工具編號 = 手工具編號;
            this.歸還日期 = 歸還日期;
            this.項次 = 項次;
            this.領用人 = 領用人;
            this.領用日期 = 領用日期;
            this.領用部門 = 領用部門;
            this.領用數量 = 領用數量;
            this.str = str;
        }

        private void bt_歸還_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                SqlCommand cmd0;
                int check1 = Int32.Parse(txt_領用數量.Text.ToString());
                int check2 = Int32.Parse(txt_歸還數量.Text.ToString());
                if (check1 < check2 || check2 <= 0)
                {
                    MessageBox.Show("0 (<) 歸還數量（<=）" + check1);
                    con.Close();
                }
                else
                {
                    cmd0 = new SqlCommand("sp_手工具領用_return", con);
                    if (str.Equals("借用"))
                        cmd0 = new SqlCommand("sp_手工具借用_return", con);
                    cmd0.CommandType = CommandType.StoredProcedure;
                    cmd0.Parameters.AddWithValue("@手工具編號", txt_手工具編號.Text.ToString());
                    cmd0.Parameters.AddWithValue("@項次", txt_項次.Text.ToString());
                    cmd0.Parameters.AddWithValue("@歸還數量", check2);
                    cmd0.ExecuteNonQuery();
                    ////MessageBox.Show("歸還成功");
                    //cmd0 = new SqlCommand("sp_手工具_手工具領用_return", con);
                    //cmd0.CommandType = CommandType.StoredProcedure;
                    //cmd0.Parameters.AddWithValue("@手工具編號", txt_手工具編號.Text.ToString());
                    //cmd0.Parameters.AddWithValue("@歸還數量", check2);
                    //cmd0.ExecuteNonQuery();
                    ////
                    //cmd0 = new SqlCommand("sp_手工具領用_return_delete", con);
                    //cmd0.CommandType = CommandType.StoredProcedure;
                    //cmd0.ExecuteNonQuery();
                    MessageBox.Show("歸還成功");
                    con.Close();
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void ManualTool_LingYong_Return_Load(object sender, EventArgs e)
        {
            if (str.Equals("借用"))
            {
                this.Text = "借用歸還";
                lb_人.Text = "借用人";
                lb_數量.Text = "借用數量";
                lb_日期.Text = "借用日期";
                lb_部門.Text = "借用部門";
            }
            txt_受理人.Text = 受理人;
            txt_手工具名稱.Text = 手工具名稱;
            txt_手工具編號.Text = 手工具編號;
            txt_歸還日期.Text = 歸還日期;
            txt_項次.Text = 項次;
            txt_領用人.Text = 領用人;
            txt_領用數量.Text = 領用數量;
            txt_領用日期.Text = 領用日期;
            txt_領用部門.Text = 領用部門;
        }

        private void bt_離開_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_歸還數量_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) || e.KeyChar == '.')
            {
                e.Handled = true;
                MessageBox.Show("很抱歉，請您輸入號碼", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
